package com.cskaoyan.user.service.impl;

import com.alibaba.nacos.common.utils.Md5Utils;
import com.cskaoyan.user.converter.MemberConverter;
import com.cskaoyan.user.dal.entitys.Member;
import com.cskaoyan.user.dal.persistence.MemberMapper;
import com.cskaoyan.user.dto.UserRegisterRequest;
import com.cskaoyan.user.dto.UserRegisterResponse;
import com.cskaoyan.user.dto.UserVerifyRequest;
import com.cskaoyan.user.dto.UserVerifyResponse;
import com.cskaoyan.user.service.IUserRegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author: 叶熠彬
 * @description:
 * @date: 2022/8/1 10:44
 */
@Service
public class UserRegisterServiceImpl implements IUserRegisterService {

    @Autowired
    MemberMapper memberMapper;
    @Autowired
    JavaMailSender mailSender;

    /**
     * 用户注册
     *
     * @author: 叶熠彬
     * @date: 2022/8/1 10:56
     * @param: [request]
     * @return: com.cskaoyan.user.dto.UserRegisterResponse
     */
    @Override
    public UserRegisterResponse register(UserRegisterRequest request) {
        UserRegisterResponse response = new UserRegisterResponse();
        try {
            request.requestCheck();
            String email = request.getEmail();
            Member member = new Member();
            member.setEmail(email);
            member.setIsVerified("N");
            // 密码MD5加密
            member.setPassword(Md5Utils.getMD5(request.getUserPwd().getBytes()));
            member.setUsername(request.getUserName());
            Date date = new Date();
            member.setCreated(date);
            member.setUpdated(date);
            // 写入数据库
            memberMapper.insertSelective(member);
            SimpleMailMessage message = new SimpleMailMessage();
            // TODO

//            message
//            mailSender.send;

        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * 邮箱认证激活
     *
     * @author: 叶熠彬
     * @date: 2022/8/1 11:09
     * @param: [request]
     * @return: com.cskaoyan.user.dto.UserVerifyResponse
     */
    @Override
    public UserVerifyResponse verify(UserVerifyRequest request) {
        return null;
    }
}
